//
//  MediumView.swift
//  MySwiftUIDemo
//
//  Created by wangws1990 on 2024/7/8.
//

import SwiftUI


struct MediumView: View {
    @State var index: Int = 0
    //最少7个
    @State var listInt: [String] = [] //["icon_puzz_type_sub7","icon_puzz_type_none","icon_puzz_type_sub1","icon_puzz_type_sub2","icon_puzz_type_sub3","icon_puzz_type_sub4","icon_puzz_type_sub5","icon_puzz_type_sub6","icon_puzz_type_sub7 ","icon_puzz_type_sub6 "]
    @ViewBuilder
    func getSechmenet()->some View{
        HStack(spacing: 0, content: {
            ZStack{
                Text("-").padding()
            }
            .onTapGesture {
                var list = listInt
                if let item = list.last{
                    list.removeLast()
                    list.insert(item, at: 0)
                }
                listInt = list
                debugPrint(listInt)
                index = index - 1
            }
            ZStack{
                Text("+").padding()
            }
            .onTapGesture {
                var list = listInt
                if let item = list.first{
                    list.removeFirst()
                    list.append(item)
                }
                listInt = list
                index = index + 1
                debugPrint(listInt)
            }
        })
    }
    var listData: [String]{
        let list : [String] = ["icon_puzz_type_sub1","icon_puzz_type_sub2","icon_puzz_type_sub3","icon_puzz_type_sub4","icon_puzz_type_sub5"]//6个
        debugPrint(list.prefix(4),list.suffix(4))
        var listNew: [String] = []
        if list.count <= 7{
            if list.count == 7{// 6 7 1 2 3 4 5
                listNew = list
                let item7 = list[6]
                let item6 = list[5]
                listNew.removeLast()
                listNew.removeLast()
                listNew.insert(item7, at: 0)
                listNew.insert(item6, at: 0)
            }
            else if list.count == 6{ // 1 2 3 4 5 6 -  3 6 1 2 3 4 5
                listNew = list
                let item5 = list[5]
                let item4 = list[2]
                listNew.removeLast()
                listNew.insert(item5, at: 0)
                listNew.insert(item4, at: 0)
            }else if list.count == 5{// 1 2 3 4 5 - 4 5 1 2 3 4 5
                let item3 = list[3]
                let item4 = list[4]
                listNew.append("\(item3)==0")
                listNew.append("\(item4)==1")
                listNew.append(contentsOf: list)
                
            }else if list.count == 4{// 1 2 3 4  //3 4 1 2 3 4 1
                let item0 = list[0]
                let item2 = list[2]
                let item3 = list[3]
                
                listNew.append("\(item2)==0")
                listNew.append("\(item3)==1")
                listNew.append(contentsOf: list)
                listNew.append("\(item0)==2")
                
            }else if list.count == 3{//2 3 1 2 3 1 2
               let item0 = list[0]
               let item1 = list[1]
               let item2 = list[2]
                listNew.append("\(item1)==0")
                listNew.append("\(item2)==1")
                listNew.append(contentsOf: list)
                listNew.append("\(item0)==2")
                listNew.append("\(item1)==3")
            }
        }
        return listNew
    }
    func getIndex(_ objc: String)->Int{
        return listInt.firstIndex { item in
            return item == objc
        } ?? 0
    }
    var body: some View {
        ZStack{
            VStack{
                getSechmenet()
                //右移动
                let last = listInt.last
                let first = listInt.first
                ZStack{
                    HStack(spacing: 10, content: {
                        ForEach(listInt,id: \.self) { objc in
                            ZStack{
                                if objc == last{
                                    getContent(objc)
                                        
                                }else if objc == first{
                                    getContent(objc)
                                }else{
                                    getContent(objc)
                                }
                            }
                        }
                    })
                    .animation(.linear, value: index)
                    .offset(x: CGFloat((listInt.count - 7)*50))
                }.frame(width: 330, height: 150, alignment: .center).clipped()

            }

        }.onAppear(perform: {
            listInt = listData
            debugPrint(listData)
        })
    }
    @ViewBuilder
    func getContent(_ objc :String)->some View{
        if let img = objc.components(separatedBy: "=").first{
            Image(img).resizable().scaledToFill().frame(width: 100,height: 100)
                .rotationEffect(.degrees(Double(getIndex(objc)%2 == 0 ? 3 : -3)))
        }
    }
}


struct LargeView: View {
    @State var index: Int = 0
    
//    @State var listInt: [String] = ["icon_puzz_type_sub6","icon_puzz_type_sub7","icon_puzz_type_sub1","icon_puzz_type_sub2","icon_puzz_type_sub3","icon_puzz_type_sub4","icon_puzz_type_sub5"]

    @State var listData: [[String]] = [] //[["icon_puzz_type_sub1","icon_puzz_type_sub2"],["icon_puzz_type_sub3","icon_puzz_type_sub4"],["icon_puzz_type_sub5","icon_puzz_type_sub6"],[" icon_puzz_type_sub1","icon_puzz_type_sub2"],[" icon_puzz_type_sub3","icon_puzz_type_sub4"],[" icon_puzz_type_sub5","icon_puzz_type_sub6"]]
    
    var listDataNew: [String]{
        let list : [String] = ["icon_puzz_type_sub1","icon_puzz_type_sub2","icon_puzz_type_sub3","icon_puzz_type_sub4"]//6个
        var listNew: [String] = []
        if list.count <= 12{
            if list.count == 10{
                let last4 = list.suffix(4)
                listNew = last4 + list
                listNew.removeLast()
                listNew.removeLast()
            }
            else if list.count == 9{
                listNew = listNew.suffix(4) + list
                listNew.removeLast()
            }
            else if list.count == 8{  //5 6 7 8 1 2 3 4 5 6 7 8
                listNew = list.suffix(4) + list
                
            }else if list.count == 7{//  4 5 6 7  1 2 3 4 5 6 7  1
                listNew = list.suffix(4) + list + list.prefix(1)
            }
            else if list.count == 6{ // 3 4 5 6 1 2 3 4 5 6 -  1 2
                listNew = list.suffix(4) + list + list.prefix(2)
            }else if list.count == 5{// 1 2 3 4 5 // 2 3 4 5  - 1 2 3 4 - 5 1 2 3 4
                listNew = list.suffix(4) + list + list.prefix(3)
                
            }else if list.count == 4{// 1 2 3 4  // 1 2 3 4 1 2 3 4 1 2 3 4
                listNew = list + list + list
            }
        }
        return listNew
    }
    
    
    @ViewBuilder
    func getSechmenet()->some View{
        HStack(spacing: 0, content: {
            ZStack{
                Text("-").padding()
            }
            .onTapGesture {
                var list = listData
                if let item = list.last{
                    list.removeLast()
                    list.insert(item, at: 0)
                }
                listData = list
                index = index - 1
            }
            ZStack{
                Text("+").padding()
            }
            .onTapGesture {
                var list = listData
                if let item = list.first{
                    list.removeFirst()
                    list.append(item)
                }
                listData = list
                index = index + 1
            }
        })
        .onAppear {
            var list : [String] = []
            for (i,objc) in listDataNew.enumerated(){
                list.append("\(objc)=\(i)")
                if i % 2 == 1{
                    listData.append(list)
                    list = []
                }
            }
        }
    }
    func getIndex(_ objc: [String])->Int{
        return listData.firstIndex { item in
            return item == objc
        } ?? 0
    }
    var body: some View {
        ZStack{
            VStack{
                getSechmenet()
                //右移动
                let last = listData.last
                let first = listData.first
                ZStack{
                    HStack(spacing: 10, content: {
                        ForEach(listData,id: \.self) { objc in
                            ZStack{
                                if objc == last{
                                    
                                    getContent(objc)
                                        
                                }else if objc == first{
                                    getContent(objc)
                                }else{
                                    getContent(objc)
                                }

                            }
                        }
                    })
                    .animation(.spring, value: index)
                }.frame(width: 220, height: 250, alignment: .center).clipped()

            }

        }
    }
    @ViewBuilder
    func getContent(_ objc :[String]) ->some View{
        VStack(spacing: 10) {
            ZStack{
                if let img = objc.first{
                    if let image = img.components(separatedBy: "=").first{
                        Image(image).resizable().scaledToFill().frame(width: 100,height: 100)
                    }
                }
            }
            .rotationEffect(.degrees(Double(getIndex(objc)%2 == 0 ? -3 : 3)))
            ZStack{
                if let img = objc.last{
                    if let image = img.components(separatedBy: "=").first{
                        Image(image).resizable().scaledToFill().frame(width: 100,height: 100)
                    }
                }
            }
            .rotationEffect(.degrees(Double(getIndex(objc)%2 == 0 ? 3 : -3)))
        }
    }
}
